<template>
  <div>
    <template class="c-field" v-if="display">{{ display }}</template>
    <template v-else>
      <span class="c-field-placeholder">{{ placeholder }}</span>
    </template>
    <van-popup
      v-model="visible"
      position="bottom"
      get-container="body"
      class="c-hfni2f"
    >
      <div v-if="!multi">
        <!-- 单选 -->
        <div class="c-hfni2f-title">{{ title }}</div>
        <div
          class="c-hfni2f-item"
          v-for="item in columns"
          :key="item.index"
          :class="item.code == value ? 'c-hfni2f-item-active' : ''"
          @click="handleClick(item)"
        >
          {{ item.codeName }}
        </div>
      </div>
      <div v-else>
        <!-- 多选 -->
        <div class="flex-row flex-align-center flex-justify-between">
          <div class="c-hfni2f-title">{{ title }}</div>
          <div class="c-hfni2f-title-txt" @click="handleConfirm">确定</div>
        </div>
        <div
          class="c-hfni2f-item"
          v-for="item in columns"
          :key="item.index"
          :class="
            multiSelectList.includes(item.code) ? 'c-hfni2f-item-active' : ''
          "
          @click="handleClick(item)"
        >
          {{ item.codeName }}
        </div>
      </div>
    </van-popup>
  </div>
</template>
<script>
export default {
  props: {
    title: {
      type: String,
      default: ''
    },
    placeholder: {
      type: String,
      default: ''
    },
    defaultValue: {
      type: [String, Number, Array],
      default: ''
    },
    inputType: {
      type: String,
      default: '' //bank银行 ，card证件号， relation是投保人的
    },
    backType: {
      // 返回数据类型
      type: String,
      default: '' // obj 返回为对象
    },
    multi: {
      // 是否支持多选
      type: Boolean,
      default: false
    },
    columns: {
      type: Array,
      default: () => []
    }
  },
  watch: {
    defaultValue(val) {
      if (this.multi) {
        this.multiValue = val
        this.multiSelectList = JSON.parse(JSON.stringify(val))
      } else {
        this.value = val
      }
    }
  },
  computed: {
    display() {
      if (this.multi) {
        let data = []
        this.columns.map(item => {
          if (this.multiValue.includes(item.code)) {
            data.push(item.codeName)
          }
        })
        let nameString = data.toString()
        return nameString
      } else {
        const item = this.columns.find(item => item.code == this.value)
        return item ? item.codeName : ''
      }
    }
  },
  data() {
    return {
      visible: false,
      value: '',
      multiValue: [],
      multiSelectList: []
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      if (this.multi) {
        this.multiValue = this.defaultValue
        this.multiSelectList = JSON.parse(JSON.stringify(this.defaultValue))
      } else {
        this.value = this.defaultValue
      }
    },
    handleClick(item) {
      // console.log(111,item)
      if (this.multi) {
        this.multipleSelect(item)
      } else {
        this.singleSelect(item)
      }
    },
    singleSelect(item) {
      if (this.backType === 'obj') {
        this.$emit('confirm', item)
      } else {
        this.$emit('confirm', item.code)
      }
      this.value = item.code
      this.visible = false
    },
    multipleSelect(item, index) {
      // const value = this.multiValue.find(findItem => findItem == item.code)
      // if (value) {
      //   this.multiValue.splice(index, 1)
      // } else {
      //   this.multiValue.push(item.code)
      // }
      const value = this.multiSelectList.find(findItem => findItem == item.code)
      if (value) {
        this.multiSelectList.splice(index, 1)
      } else {
        this.multiSelectList.push(item.code)
      }
    },
    handleConfirm() {
      this.multiValue = JSON.parse(JSON.stringify(this.multiSelectList))
      this.$emit('confirm', this.multiValue)
      this.visible = false
    }
  }
}
</script>
<style lang="less" scoped>
.c-field {
  font-size: 15px;
  color: #333;
}
.c-field-placeholder {
  color: #999;
  font-size: 15px;
}
.c-hfni2f {
  max-height: 500px;
  border-radius: 13px 13px 0 0;
  text-align: center;
  padding: 15px;
  box-sizing: border-box;
  &-title {
    font-size: 18px;
    color: #333;
    font-weight: bold;
    &-txt {
      font-size: 14px;
      color: @c-color-primary;
    }
  }
  &-item {
    font-size: 15px;
    color: #666;
    margin-top: 15px;
    padding-bottom: 10px;
    border-bottom: 0.5px solid #e9e9e9;
    &:last-child {
      border-bottom: none;
    }
  }
  &-item-active {
    color: @c-color-primary;
  }
}
</style>
